package com.eleadmin.order.controller;

import com.eleadmin.common.core.web.*;
import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.ocean.entity.OceanBulkCargo;
import com.eleadmin.order.entity.ContainerBill;
import com.eleadmin.order.entity.Customer;
import com.eleadmin.order.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.formula.functions.T;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 *
 * 客户管理
 * @author : Doman
 * @date : 2023-02-13 15:50:11
 * @version ：1.0
 */

@Api(tags = "客户管理")
@RestController
@RequestMapping("/api/order/customer")
public class CustomerController extends BaseController {

    @Autowired
    private CustomerService customerService;

    @PreAuthorize("hasAuthority('order:customer:list')")
    @ApiOperation("分页查询客户管理")
    @GetMapping("/page")
    public JsonResult page(PageResult2<T> page, Customer customer) throws Exception{
        return customerService.list(page, customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:customer:list')")
    @ApiOperation("查询全部客户管理")
    @GetMapping()
    public JsonResult list(Customer customer){
        return customerService.list(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:customer:list')")
    @ApiOperation("返回客户select列表")
    @GetMapping("listSel")
    public JsonResult listSel(Customer customer){
        return customerService.listSel(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:customer:list')")
    @ApiOperation("返回开票select列表")
    @GetMapping("invoiceSel")
    public JsonResult invoiceSel(Customer customer){
        return customerService.invoiceSel(customer, getLoginUser());
    }

    @PreAuthorize("hasAuthority('order:customer:list')")
    @ApiOperation("根据id查询客户管理")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") String id) {
        return customerService.getById(id);
    }


    @PreAuthorize("hasAuthority('order:customer:save')")
    @OperationLog
    @ApiOperation(value ="添加客户管理")
    @PostMapping()
    public JsonResult save(@RequestBody Customer customer) {
        try {
            return customerService.saveCustomer(customer, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('order:customer:exam')")
    @OperationLog
    @ApiOperation(value ="审核客户管理")
    @PostMapping("examCustomer")
    public JsonResult examCustomer(@RequestBody Customer customer) {
        try {
            return customerService.examCustomer(customer, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('order:customer:update')")
    @OperationLog
    @ApiOperation(value ="修改客户管理")
    @PutMapping()
    public JsonResult update(@RequestBody Customer customer) {
        try {
            return customerService.update(customer, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('order:customer:remove')")
    @OperationLog
    @ApiOperation(value ="删除客户管理")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") String id) {
        try {
            return customerService.remove(id, getLoginUser());
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }


    @PreAuthorize("hasAuthority('order:customer:exportsCustomer')")
    @ApiOperation("导出客户拖车账单")
    @GetMapping("/exportsCustomerContainerBill")
    public void exportsCustomerContainerBill(ContainerBill containerBill, HttpServletResponse response, HttpServletRequest request) throws Exception{
        customerService.exportsCustomerContainerBill(containerBill, getLoginUser(),response, request);
    }

    @PreAuthorize("hasAuthority('order:customer:exportsCustomer')")
    @ApiOperation("导出客户散货账单")
    @GetMapping("/exportsCustomerOceanBulkCargo")
    public void exportsCustomerOceanBulkCargo(OceanBulkCargo oceanBulkCargo, HttpServletResponse response, HttpServletRequest request) throws Exception{
        customerService.exportsCustomerOceanBulkCargo(oceanBulkCargo, getLoginUser(),response, request);
    }

    @PreAuthorize("hasAuthority('order:customer:exportsCustomer')")
    @ApiOperation("导出客户散货账单")
    @GetMapping("/exportsCustomerOceanBulkCargo2")
    public void exportsCustomerOceanBulkCargo2(OceanBulkCargo oceanBulkCargo, HttpServletResponse response, HttpServletRequest request) throws Exception{
        customerService.exportsCustomerOceanBulkCargo2(oceanBulkCargo, getLoginUser(),response, request);
    }

}
